

//
//  meInterrestTableVC.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/18.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit

class LDBossActivenessVC11: LDBaseCustomizeVC {
    
    
    var headImgView:UIImageView?
    var labActiveNum:UILabel?
    var labActiveText:UILabel?
    var labActiveDesc:UILabel?
    
    var headImgW:CGFloat = IPHONE6UP ? 80:60
    
    lazy var contentViewScroll:UIScrollView = {
        
        let contentview = UIScrollView.init(frame: LDFrame.ScreenBounds)
        contentview.backgroundColor = LDBackGoundColorGray
        
        contentview.scrollEnabled = true
        contentview.showsVerticalScrollIndicator = false
        
        
        contentview.contentSize = CGSizeMake(LDFrame.ScreenWidth, LDFrame.ScreenHeight - LDFrame.TabbarH)//
        
        
        return contentview
    }()
    //遮罩
    lazy var coverView:UIView = {
        let cover = UIView.init()
        cover.backgroundColor = LDFontColor.White
        cover.layer.cornerRadius = 2
        cover.layer.shadowRadius = 2
        cover.layer.masksToBounds = true
        return cover
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "活跃度"
        setDefaultContent()
        setupFrame()
    }
    
    
    func updata() {
       
        DDBossInfoService().queryBossInfoWithRefresh(true) { [weak self] (info) in
            
            let model:DDBossInfo = info
            
            let picurl = LDCommentAPI.getPicApi(model.logo ?? "")
            self?.headImgView?.sd_setImageWithURL(NSURL(string: picurl), placeholderImage: UIImage(named: "default_header"))
            
            self?.labActiveNum?.text = model.activeness
            
            let actNo = String(format: "%@", model.attention ?? "0")
            let str:NSString  = "活跃度越高 , 排名越靠前 , 越容易被Boss关注\n你的被关注度超越了\(actNo)%的Boss"
            let attr = self?.getDescAttr(str)
            self?.labActiveDesc?.attributedText = attr
            
            
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        updata()
        rdv_tabBarController.setTabBarHidden(true, animated: true)
    }
    
    func setDefaultContent() {
        
        headImgView = UIImageView.setDefaultGoundImg("default_header", radius: headImgW/2)
        labActiveNum = UILabel.setDetailsLab("0", fontSize: 30, titleColor: LDFontColor.Red,Alignment: .Center)
        
        labActiveText = UILabel.setDetailsLab("近三天的活跃度", fontSize: 16, titleColor: LDFontColor.Red,Alignment: .Center)
        
        let str:NSString  = "活跃度越高 , 排名越靠前 , 越容易被牛人 关注\n你的被关注度超越了82%的Boss"
        
        let attr = getDescAttr(str)
        
        labActiveDesc = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.DeepGray,Alignment: NSTextAlignment.Center)
        labActiveDesc?.attributedText = attr
        labActiveDesc?.numberOfLines = 0
        
        if IPHONE6UP == false {
            contentViewScroll.contentSize = CGSizeMake(LDFrame.ScreenWidth, 700)
        }
        
        view.addSubview(contentViewScroll)
        
        contentViewScroll.addSubview(headImgView!)
        contentViewScroll.addSubview(labActiveNum!)
        contentViewScroll.addSubview(labActiveText!)
        contentViewScroll.addSubview(labActiveDesc!)
        contentViewScroll.addSubview(coverView)
    }
    
    func getDescAttr(str:NSString) ->NSAttributedString{
        
        let strLocation:NSString = "的Boss"
        //定位位置
        let range = str.rangeOfString(strLocation as String)
        //放大的位置
        let rangeLoaction = NSRange(location: range.location - 3, length: 3)
        
        let attrTxt = NSMutableAttributedString(string: str as String)
        attrTxt.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(19), range: rangeLoaction)
        return attrTxt
    }
    
    func setupFrame(){
        
        /** 水平分割线 */
        let divideLine1 = UIView.init()
        divideLine1.backgroundColor = LDLineColorLightGray
        /** 竖直分割线 */
        let divideLine2 = UIView.init()
        divideLine2.backgroundColor = LDLineColorLightGray
        
        
        let titleArr = [["每日签到","10活跃度/次","签到"],
                        ["每日沟通","20活跃度/次","前往"],
                        ["看看新牛人","2活跃度/次","前往"],
                        ["分享微主页","10活跃度/次","分享"]
                        ]
        
        /**签到*/
        let view1 = LDMyActiveMainBottomView()
        view1.titleParams = titleArr[0]
        view1.actionBtn?.bk_whenTapped({ //[weak self] in
            
            DDBossInfoService().addActivenessWithType("signin", callback: {
                [weak self](finished, obj) in
            
                if finished == true {
                
                    self?.updata()
                    LDShowHUB.shareInstance.showToastNoti("签到成功")
                    
                } else {
                    LDShowHUB.shareInstance.showToastNoti(obj as? String ?? "签到失败")
                }
            })
        })
        
        //沟通
        let view2 = LDMyActiveMainBottomView()
        view2.titleParams = titleArr[1]
        view2.actionBtn?.bk_whenTapped({ [weak self] in
            
            let vc = DDNewCattleViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
        })
        
        //看看新牛人
        let view3 = LDMyActiveMainBottomView()
        view3.titleParams = titleArr[2]
        view3.actionBtn?.bk_whenTapped({ [weak self] in
            
            let vc = DDNewCattleViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
        })
        //分享微主页
        let view4 = LDMyActiveMainBottomView()
        view4.titleParams = titleArr[3]
        view4.actionBtn?.bk_whenTapped({ [weak self] in
            
            DDBossInfoService().addActivenessWithType("share", callback: { (finished, obj) in
                
                if finished == true {
                    
                } else {
                    
                    LDShowHUB.shareInstance.showToastNoti(obj as? String ?? "分享失败")
                }
                
                let vc = LDMicroCompanyPage()//DDCompanyMicroPageViewController()
                self?.navigationController?.pushViewController(vc, animated: true)
                
            })
        })
        
        coverView.addSubview(divideLine1)
        coverView.addSubview(divideLine2)
        
        coverView.addSubview(view1)
        coverView.addSubview(view2)
        coverView.addSubview(view3)
        coverView.addSubview(view4)
        //
        
        //自动布局
        headImgView?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(Margin_V * 6)
            make.centerX.equalTo(contentViewScroll)
            make.width.height.equalTo(headImgW)
        })
        labActiveNum?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((headImgView?.snp_bottom)!).offset(Margin_V*3)
            make.centerX.equalTo(headImgView!)
            make.size.equalTo(CGSizeMake(100, 45))
        })
        labActiveText?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((labActiveNum?.snp_bottom)!).offset(0)//Margin_V)
            make.centerX.equalTo(headImgView!)
            make.size.equalTo(CGSizeMake(120, 25))
        })
        labActiveDesc?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((labActiveText?.snp_bottom)!).offset(Margin_V)
            make.centerX.equalTo(headImgView!)
            make.size.equalTo(CGSizeMake(LDFrame.ScreenWidth, 60))
        })
        //*********** 底部视图布局 ************//
        //遮罩视图
        coverView.snp_makeConstraints { (make) in
            make.top.equalTo((labActiveDesc?.snp_bottom)!).offset(Margin_V*2)
            
            make.left.equalTo(Margin_V*3)
            make.right.equalTo(view.snp_right).offset(-Margin_V*3)
            make.height.equalTo(240)
        }
        
        //分割线 divideLine1 水平方向
        divideLine1.snp_makeConstraints { (make) in
            make.center.equalTo(coverView)
            make.height.equalTo(0.5)
            make.width.equalTo(coverView).offset(-2)
        }
        divideLine2.snp_makeConstraints { (make) in
            make.center.equalTo(coverView)
            make.height.equalTo(coverView).offset(-2)
            make.width.equalTo(0.5)
        }
        
        //小方格布局
        view1.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(coverView)
            make.left.equalTo(0)
            make.right.equalTo(divideLine2)
            make.bottom.equalTo(divideLine1)
        })
        view2.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(view1)
            make.left.equalTo(divideLine2)
            make.right.equalTo(coverView)
        })
        view3.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(divideLine1)
            make.left.right.equalTo(view1)
            make.bottom.equalTo(coverView)
        })
        view4.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(view3)
            make.left.equalTo(divideLine2)
            make.right.equalTo(coverView)
        })
        
    }
}


class LDBossActivenessVC: LDBaseCustomizeVC {
    
    
    var headImgView:UIImageView?
    var labActiveNum:UILabel?
    var labActiveText:UILabel?
    var labActiveDesc:UILabel?
    var model:DDUserInfoModel?
    var divideLine:UIView?
    var topCover:UIView?
    
    var headImgW:CGFloat = IPHONE6UP ? 80:60
    
    lazy var contentViewScroll:UIScrollView = {
        
        let contentview = UIScrollView.init(frame: LDFrame.ScreenBounds)
        contentview.backgroundColor = LDBackGoundColorGray
        contentview.contentSize = CGSizeMake(LDFrame.ScreenWidth, LDFrame.ScreenHeight - LDFrame.TabbarH)//
        
        contentview.scrollEnabled = true
        contentview.showsVerticalScrollIndicator = false
        
        
        return contentview
    }()
    //遮罩
    lazy var coverView:UIView = {
        let cover = UIView.init()
        cover.backgroundColor = LDFontColor.White
        cover.layer.cornerRadius = 2
        cover.layer.shadowRadius = 2
        cover.layer.masksToBounds = true
        return cover
    }()
    
    lazy var shareView:DDShareView = {
        
        let share = NSBundle.mainBundle().loadNibNamed("DDShareView", owner: self, options: nil).last
        
        return share as? DDShareView ?? DDShareView()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "活跃度"
        setDefaultContent()
        setupFrame()
    }
    
    override func viewWillAppear(animated: Bool) {
        updata()
        rdv_tabBarController.setTabBarHidden(true, animated: true)
    }
    
    
    func updata() {
        
        
        DDBossInfoService().queryBossInfoWithRefresh(true) { [weak self] (info) in
            
            let model:DDBossInfo = info
            
            let picurl = LDCommentAPI.getPicApi(model.logo ?? "")
            self?.headImgView?.sd_setImageWithURL(NSURL(string: picurl), placeholderImage: UIImage(named: "default_header"))
            
            self?.labActiveNum?.text = model.activeness
            
            let actNo = String(format: "%@", model.attention ?? "0")
            let str:NSString  = "近三天的活跃度 \n\n活跃度越高 , 排名越靠前 , 越容易被牛人关注\n你的被关注度超越了\(actNo)%的Boss"
            let attr = self?.getDescAttr(str)
            self?.labActiveDesc?.attributedText = attr
            
        }
    }
    
    
    
    func setDefaultContent() {
        
        headImgView = UIImageView.setDefaultGoundImg("default_header", radius: headImgW/2)
        headImgView?.layer.borderWidth = 1;
        headImgView?.layer.borderColor = LDFontColor.Green.CGColor
        
        labActiveNum = UILabel.setDetailsLab("0", fontSize: 30, titleColor: LDFontColor.Green,Alignment: .Left)
        
        labActiveText = UILabel.setDetailsLab("近三天的活跃度", fontSize: 14, titleColor: LDFontColor.Green,Alignment: .Left)
        divideLine = UIView()
        divideLine?.backgroundColor = LDLineColorLightGray
        
        let str:NSString  = "近三天的活跃度 \n\n活跃度越高 , 排名越靠前 , 越容易被牛人关注\n你的被关注度超越了0%的Boss"
        
        let attr = getDescAttr(str)
        
        labActiveDesc = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.DeepGray,Alignment: NSTextAlignment.Left)
        labActiveDesc?.attributedText = attr
        labActiveDesc?.numberOfLines = 0
        
        
        if IPHONE6UP == false {
            
            contentViewScroll.contentSize = CGSizeMake(LDFrame.ScreenWidth, 700)
        }
        
        view.addSubview(contentViewScroll)
        
        //2.0headcover
        topCover = UIView()
        topCover!.backgroundColor = UIColor.whiteColor()
        
        topCover!.addSubview(divideLine!)
        topCover!.addSubview(labActiveDesc!)
        topCover!.addSubview(headImgView!)
        topCover!.addSubview(labActiveNum!)
        topCover!.addSubview(labActiveText!)
        topCover!.addSubview(labActiveDesc!)
        
        contentViewScroll.addSubview(topCover!)
        contentViewScroll.addSubview(coverView)
    }
    
    func getDescAttr(str:NSString) ->NSAttributedString{
        
        let strLocation:NSString = "的Boss"
        //定位位置
        let range = str.rangeOfString(strLocation as String)
        //放大的位置
        let rangeLoaction = NSRange(location: range.location - 3, length: 3)
        
        let attrTxt = NSMutableAttributedString(string: str as String)
        attrTxt.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(19), range: rangeLoaction)
        
        let range2 = NSRange.init(location: 0, length: 7)
        attrTxt.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(16),NSForegroundColorAttributeName:LDFontColor.Black], range: range2)
        
        return attrTxt
    }
    
    func setupFrame(){
        
        /** 水平分割线 */
        let divideLine1 = UIView.init()
        divideLine1.backgroundColor = LDLineColorLightGray
        /** 竖直分割线 */
        let divideLine2 = UIView.init()
        divideLine2.backgroundColor = LDLineColorLightGray
        
        /**签到,沟通,new职位,分享*/
        
        
        
        
        let titleArr = [["每日签到","10活跃度/次","签到"],
                        ["每日沟通","20活跃度/次","前往"],
                        ["看看新牛人","2活跃度/次","前往"],
                        ["分享微主页","10活跃度/次","分享"]
        ]
        
        /**签到*/
        let view1 = LDMyActiveMainBottomView()
        view1.titleParams = titleArr[0]
        view1.actionBtn?.bk_whenTapped({ //[weak self] in
            
            DDBossInfoService().addActivenessWithType("signin", callback: {
                [weak self](finished, obj) in
                
                if finished == true {
                    
                    self?.updata()
                    LDShowHUB.shareInstance.showToastNoti("签到成功")
                    
                } else {
                    LDShowHUB.shareInstance.showToastNoti(obj as? String ?? "签到失败")
                }
                })
        })
        
        //沟通
        let view2 = LDMyActiveMainBottomView()
        view2.titleParams = titleArr[1]
        view2.actionBtn?.bk_whenTapped({ [weak self] in
            
            let vc = DDNewCattleViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
            })
        
        //看看新牛人
        let view3 = LDMyActiveMainBottomView()
        view3.titleParams = titleArr[2]
        view3.actionBtn?.bk_whenTapped({ [weak self] in
            
            let vc = DDNewCattleViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
            })
        //分享微主页
        let view4 = LDMyActiveMainBottomView()
        view4.titleParams = titleArr[3]
        view4.actionBtn?.bk_whenTapped({ [weak self] in
            
            DDBossInfoService().addActivenessWithType("share", callback: { (finished, obj) in
                
                if finished == true {
                    
                } else {
                    
                    LDShowHUB.shareInstance.showToastNoti(obj as? String ?? "分享失败")
                }
                
                let vc = LDMicroCompanyPage()//DDCompanyMicroPageViewController()
                self?.navigationController?.pushViewController(vc, animated: true)
                
            })
        })
        
        coverView.addSubview(divideLine1)
        coverView.addSubview(divideLine2)
        
        coverView.addSubview(view1)
        coverView.addSubview(view2)
        coverView.addSubview(view3)
        coverView.addSubview(view4)
        
        
        
        
        //自动布局
        
        topCover!.snp_makeConstraints { (make) in
            
            make.top.equalTo(Margin_V*3)
            make.size.equalTo(CGSizeMake(LDFrame.ScreenWidth, IPHONE6UP ? 235 : 215))
        }
        
        headImgView?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(Margin_V * 4)
            make.left.equalTo(30)
            make.width.height.equalTo(headImgW)
        })
        labActiveNum?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(headImgView!.snp_top).offset(Margin_V*2)
            make.left.equalTo(headImgView!.snp_right).offset(Margin_V*3)
            make.size.equalTo(CGSizeMake(100, 40))
        })
        labActiveText?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo((labActiveNum?.snp_bottom)!)//.offset(Margin_V)
            make.left.equalTo(labActiveNum!)
            make.size.equalTo(CGSizeMake(120, 15))
        })
        
        divideLine?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(headImgView!.snp_bottom).offset(Margin_V*3)
            make.height.equalTo(1)
            make.centerX.equalTo(contentViewScroll)
            make.width.equalTo(LDFrame.ScreenWidth - 25*2)
        })
        
        labActiveDesc?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo((divideLine?.snp_bottom)!).offset(Margin_V)
            make.left.equalTo(headImgView!)
            make.size.equalTo(CGSizeMake(LDFrame.ScreenWidth, 100))
        })
        
        
        
        //*********** 底部视图布局 ************//
        //遮罩视图
        coverView.snp_makeConstraints { (make) in
            make.top.equalTo((topCover?.snp_bottom)!).offset(Margin_V*3)
            
            make.left.equalTo(Margin_V*3)
            make.right.equalTo(view.snp_right).offset(-Margin_V*3)
            make.height.equalTo(240)
        }
        
        //分割线 divideLine1 水平方向
        divideLine1.snp_makeConstraints { (make) in
            make.center.equalTo(coverView)
            make.height.equalTo(0.5)
            make.width.equalTo(coverView).offset(-2)
        }
        divideLine2.snp_makeConstraints { (make) in
            make.center.equalTo(coverView)
            make.height.equalTo(coverView).offset(-2)
            make.width.equalTo(0.5)
        }
        
        //小方格布局
        view1.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(coverView)
            make.left.equalTo(0)
            make.right.equalTo(divideLine2)
            make.bottom.equalTo(divideLine1)
        })
        view2.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(view1)
            make.left.equalTo(divideLine2)
            make.right.equalTo(coverView)
        })
        view3.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(divideLine1)
            make.left.right.equalTo(view1)
            make.bottom.equalTo(coverView)
        })
        view4.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(view3)
            make.left.equalTo(divideLine2)
            make.right.equalTo(coverView)
        })
        
    }
}
